﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Avatar.ClientDevice;

namespace Avatar.Client.App
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        string startStop = "开始";
        ObservableCollection<string> messages = new ObservableCollection<string>();
        public MainPage()
        {
            InitializeComponent();
            listMsg.ItemsSource = messages;
            pkType.SelectedIndex = 0;
            SunVoteManager.OnConnect += Manager_OnConnect;
            SunVoteManager.OnVote += Manager_OnVote;
            SunVoteManager.OnKey += Manager_OnKey;
            SunVoteManager.OnLog += Manager_OnLog;
        }

        private void Manager_OnLog(string obj)
        {
            System.Diagnostics.Debug.WriteLine(obj);
        }

        private void Manager_OnKey(SunVoteEventArgs param)
        {
            if (MainThread.IsMainThread)
            {
                OnKeyCore(param);
            }
            else
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    OnKeyCore(param);
                });
            }
        }

        private void OnKeyCore(SunVoteEventArgs param)
        {
            messages.Add($"键盘:{param.keySN}, sInfo:{param.sInfo}");
        }

        private void Manager_OnVote(SunVoteEventArgs param)
        {
            if (MainThread.IsMainThread)
            {
                OnVoteCore(param);
            }
            else
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    OnVoteCore(param);
                });
            }
        }

        private void OnVoteCore(SunVoteEventArgs param)
        {
            messages.Add($"{startStop}投票!");
        }

        private void Manager_OnConnect(SunVoteEventArgs param)
        {
            if (MainThread.IsMainThread)
            {
                OnConnectCore(param);
            }
            else
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    OnConnectCore(param);
                });
            }
        }

        private void OnConnectCore(SunVoteEventArgs param)
        {
            if (param.sInfo == "1")
            {
                messages.Add("基站连接成功!");
            }
            else
            {
                messages.Add("基站连接失败!");
            }
        }

        private void BtnClear_Clicked(object sender, EventArgs e)
        {
            if (MainThread.IsMainThread)
            {
                ClearCore();
            }
            else
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    ClearCore();
                });
            }
        }

        private void ClearCore()
        {
            messages.Clear();
        }

        private void BtnStart_Clicked(object sender, EventArgs e)
        {
            SunVoteManager.Manager.StartChoice();
            startStop = "开始";
        }

        private void BtnEnd_Clicked(object sender, EventArgs e)
        {
            SunVoteManager.Manager.StopVote();
            startStop = "停止";
        }

        private void BtnConnect_Clicked(object sender, EventArgs e)
        {
            if (pkType.SelectedIndex == 0)
            {
                SunVoteManager.Manager.Connect_USB(1);
            }
            else
            {
                SunVoteManager.Manager.Connect_TCPIP("10.10.100.254");
            }
        }

        private void BtnDisConnect_Clicked(object sender, EventArgs e)
        {
            SunVoteManager.Manager.DisConnect();
        }

        private void BtnStart2_Clicked(object sender, EventArgs e)
        {
            //SunVoteManager.Manager.StartChoice(SunVoteManager.Manager.ConnectedBaseID);
        }

        private void BtnEnd2_Clicked(object sender, EventArgs e)
        {
            //SunVoteManager.Manager.StopVote(SunVoteManager.Manager.ConnectedBaseID);
        }
    }
}
